﻿Public Class frmsite
    Dim Strsql As String
    Dim Mysql1 As New ClsSQLhelper
    Function Load_Data(Optional ByVal Str_Search As String = "") As Boolean
        Try
            Dim DS As New DataSet
            Load_Data = False
            Strsql = "select * from site "
            If Str_Search <> "" Then Strsql = Strsql & " WHERE site like '" & Me.TxtSearch.Text & "%' "
            Strsql = Strsql & " order by active desc,site"
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_site", "site")
            Debug.Print(Strsql)
            Pn_Framework.Set_GridSetting(Me.gridsite, DS)

            Load_Data = True
            Me.Txtid.ReadOnly = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Function
    Function Load_UserinGroup(Optional ByVal Groupid As Integer = 1) As Boolean
        Try
            Dim DS As New DataSet
            Dim lcol As Integer = 0, lrow As Integer = 0
            Load_UserinGroup = False

            Strsql = "select user.user_name,user.position,department.depname from siteuser left join user on siteuser.user = user.user left join department on user.department = department.depcode where usergroup  = " & Groupid & " and siteid = " & Me.Txtid.Text
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_siteuser", "siteuser")
            Select Case Groupid
                Case 1
                    Pn_Framework.Set_GridSetting(Me.GridAdminGroup, DS)
                Case 2
                    Pn_Framework.Set_GridSetting(Me.GridSupervisorGroup, DS)
                Case 3
                    Pn_Framework.Set_GridSetting(Me.GridUserGroup, DS)
            End Select


            Load_UserinGroup = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Load_UserinGroup - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)

        End Try
    End Function
    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub

    Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNew.Click
        Me.Txtid.ReadOnly = False
        Me.BtnSave.Enabled = True
        Me.BtnDelete.Enabled = False
        Me.Txtid.Text = ""
        Me.TxtNote.Text = ""
        Me.Txtname.Text = ""
        Me.ChkActive.Checked = False
    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        Try
            If MsgBox("คุณต้องการลบข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบ") = MsgBoxResult.No Then Exit Sub
            Strsql = "Delete from site where siteid = " & Me.Txtid.Text
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            Mysql1.MySQLExecute(Strsql)
            'Refresh Data
            Load_Data()
            BtnNew_Click(sender, e)
            Me.Txtid.ReadOnly = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Try
            If Me.Txtname.Text.Trim = "" Then
                MsgBox("คุณกรอกข้อมูลไม่ครบ", MsgBoxStyle.Exclamation, "ไม่สามารถบันทึกได้")
                Exit Sub
            End If


            If MsgBox("คุณต้องการบันทึกข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการบันทึก") = MsgBoxResult.No Then Exit Sub

            Dim DS As New DataSet
            If Me.Txtid.Text = "" Then
                Strsql = "Select * from site where site= '" & Me.Txtname.Text & "'"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                DS = Mysql1.GetMYSQLDataset(Strsql, "DS_site", "site")
                If DS.Tables(0).Rows.Count <> 0 Then
                    MsgBox("รายการ นี้มีอยู่แล้วไม่สามารถบันทึกซ้ำได้อีก", MsgBoxStyle.Critical)
                    Exit Sub
                End If
                Strsql = "Insert into site (site,note) values ('ไม่ระบุ','ค่าเริ่มต้น'," & Me.Txtid.Text & ")"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql1.MySQLExecute(Strsql)




                'Add Startup value
                Strsql = "Insert into band (band_name,note,siteid) values ('ไม่ระบุ','ค่าเริ่มต้น'," & Me.Txtid.Text & ")"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql1.MySQLExecute(Strsql)

                Strsql = "Insert into manufactur (manufacturname,note,siteid) values ('ไม่ระบุ','ค่าเริ่มต้น'," & Me.Txtid.Text & ")"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql1.MySQLExecute(Strsql)

                Strsql = "Insert into section (section_name,note,siteid) values ('ไม่ระบุ','ค่าเริ่มต้น'," & Me.Txtid.Text & ")"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql1.MySQLExecute(Strsql)

                Strsql = "Insert into category (Category_name,note,siteid) values ('ไม่ระบุ','ค่าเริ่มต้น'," & Me.Txtid.Text & ")"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql1.MySQLExecute(Strsql)

                MsgBox("เพิ่มข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)
            Else

                Strsql = "update site set site= '" & Me.Txtname.Text & "', note = '" & Me.TxtNote.Text & "' ,active = " & Me.ChkActive.Checked
                Strsql = Strsql & " where siteid = " & Me.Txtid.Text.Trim
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql1.MySQLExecute(Strsql)

                'Check Startup Value

                Strsql = "select * from  band where siteid = " & Me.Txtid.Text & " and  band_name = 'ไม่ระบุ'"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                DS = Mysql1.GetMYSQLDataset(Strsql, "DS_band", "band")
                If DS.Tables(0).Rows.Count = 0 Then
                    Strsql = "Insert into band (band_name,note,siteid) values ('ไม่ระบุ','ค่าเริ่มต้น'," & Me.Txtid.Text & ")"
                    Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                    Mysql1.MySQLExecute(Strsql)
                End If

                Strsql = "select * from  section where siteid = " & Me.Txtid.Text & " and   section_name = 'ไม่ระบุ'"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                DS = Mysql1.GetMYSQLDataset(Strsql, "DS_section", "section")
                If DS.Tables(0).Rows.Count = 0 Then
                    Strsql = "Insert into section (section_name,note,siteid) values ('ไม่ระบุ','ค่าเริ่มต้น'," & Me.Txtid.Text & ")"
                    Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                    Mysql1.MySQLExecute(Strsql)
                End If

                Strsql = "select * from  manufactur where siteid = " & Me.Txtid.Text & " and   manufacturname = 'ไม่ระบุ'"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                DS = Mysql1.GetMYSQLDataset(Strsql, "DS_manufactur", "manufactur")
                If DS.Tables(0).Rows.Count = 0 Then
                    Strsql = "Insert into manufactur (manufacturname,note,siteid) values ('ไม่ระบุ','ค่าเริ่มต้น'," & Me.Txtid.Text & ")"
                    Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                    Mysql1.MySQLExecute(Strsql)
                End If

                Strsql = "select * from  category where siteid = " & Me.Txtid.Text & " and   Category_name = 'ไม่ระบุ'"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                DS = Mysql1.GetMYSQLDataset(Strsql, "DS_category", "category")
                If DS.Tables(0).Rows.Count = 0 Then
                    Dim MySectionID As Integer
                    Strsql = "select * from  section where siteid = " & Me.Txtid.Text & " and   section_name = 'ไม่ระบุ'"
                    Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                    DS = Mysql1.GetMYSQLDataset(Strsql, "DS_section", "section")
                    If DS.Tables(0).Rows.Count <> 0 Then
                        MySectionID = DS.Tables(0).Rows(0).Item("section_id").ToString()
                    End If

                    Strsql = "Insert into category (Category_name,note,siteid,sectionid) values ('ไม่ระบุ','ค่าเริ่มต้น'," & Me.Txtid.Text & "," & MySectionID & ")"
                    Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                    Mysql1.MySQLExecute(Strsql)
                End If

                MsgBox("ปรับปรุงข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)
                Me.TabctlSite.SelectedTab = TabPage1
            End If
            'Refresh Data
            Load_Data()
            BtnNew_Click(sender, e)
            Me.Txtid.ReadOnly = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub frmsite_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Data()
    End Sub

    Private Sub gridsite_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridsite.CellClick
        Try
            With gridsite
                If .RowCount = 0 Then Exit Sub
                Me.Txtid.Text = .Item(0, e.RowIndex).Value.ToString
                Me.Txtname.Text = .Item(1, e.RowIndex).Value.ToString
                Me.TxtNote.Text = .Item(2, e.RowIndex).Value.ToString
                Me.ChkActive.Checked = .Item(3, e.RowIndex).Value.ToString
            End With
            Load_UserinGroup(1)
            Load_UserinGroup(2)
            Load_UserinGroup(3)
            With Me.TabctlSite
                .SelectedTab = TabPage2
            End With
            Me.BtnSave.Enabled = True
            Me.BtnDelete.Enabled = True
            Me.Txtid.ReadOnly = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub gridband_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridsite.CellContentClick

    End Sub

    Private Sub DataGridView3_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)

    End Sub

    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
        Load_Data(Me.TxtSearch.Text)
    End Sub

    Private Sub TxtSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtSearch.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnFind_Click(sender, e)
        End If
    End Sub

    Private Sub TxtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtSearch.TextChanged

    End Sub
End Class